package com.nhh.v.controller;

import com.google.common.collect.Maps;
import com.nhh.v.anotation.ValidateHeader;
import com.nhh.v.dto.RequiredRequestHeader;
import com.nhh.v.service.ConfigService;
import com.nhh.v.service.GameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Allen.qin
 * Date: 2018/9/4
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/game")
public class GameController {


    @Autowired
    private GameService gameService;

    @Autowired
    private ConfigService configService;

    @PostMapping(value = "getArticle")
    public Mono<Map> getArticle(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "articleId") String articleId,
            @RequestParam(value = "category") String category) {

        Map<String, Object> data = gameService.getArticle(articleId, category);
        data.putAll(CommonResult.SUCCESS);
        return Mono.just(data);
    }

    @PostMapping(value = "collect")
    public Mono<Map> collect(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "gameId") String gameId) {

        if(gameService.collect(Integer.valueOf(requireHeader.getUid()), Integer.valueOf(gameId))){
            return Mono.just(CommonResult.SUCCESS);
        }else{
            return Mono.just(CommonResult.FILED);
        }
    }

    @PostMapping(value = "getCfg")
    public Mono<Map> getCfg(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader) {

        Map<String, Object> data = configService.qrHashrateCfg();
        data.putAll(CommonResult.SUCCESS);
        return Mono.just(data);
    }


    @PostMapping(value = "gameStart")
    public Mono<Map> gameStart(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "gameId") String gameId) {

        Map<String, Object> data = gameService.executeGameStart(Integer.valueOf(requireHeader.getUid()), Integer.valueOf(gameId));
        data.putAll(CommonResult.SUCCESS);
        return Mono.just(data);
    }

    @PostMapping(value = "gameEnd")
    public Mono<Map> gameEnd(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "playUid") String playUid) {

        Map<String, Object> data = gameService.executeGameEnd(playUid);
        data.putAll(CommonResult.SUCCESS);
        return Mono.just(data);
    }

    @PostMapping(value = "qrGames")
    public Mono<Map> qrGames(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "psize", required = false, defaultValue = "10") String pSize,
            @RequestParam(value = "pindex", required = false, defaultValue = "0") String pIndex) {

        HashMap<String, Object> data = Maps.newHashMap();

        Map<String, Object> map = configService.getHashRateConfig();
        if (map != null) data.putAll(map);

        data.putAll(gameService.queryGames(Long.valueOf(requireHeader.getUid()), pIndex, pSize));
        data.putAll(CommonResult.SUCCESS);

        data.put("psize", pSize);
        return Mono.just(data);
    }

    @PostMapping(value = "switchOut")
    public Mono<Map> switchOut(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "playUid") String playUid) {

        gameService.executeSwitchOut(playUid);
        return Mono.just(CommonResult.SUCCESS);
    }

    @PostMapping(value = "switchIn")
    public Mono<Map> switchIn(
            @Valid @ValidateHeader RequiredRequestHeader requireHeader,
            @RequestParam(value = "playUid") String playUid) {

        gameService.executeSwitchIn(playUid);
        return Mono.just(CommonResult.SUCCESS);
    }

}
